package com.heima.user.service.impl;

import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.user.interceptor.AppUserIdHolder;
import com.heima.user.service.ApUserBehaviorService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class ApUserBehaviorServiceImpl implements ApUserBehaviorService {
    @Autowired
    private CacheService cacheService;

    /**
     * 用户关注或者取消关注
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult userFollowOrNotFollow(UserRelationDto dto) {
        //校验参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取自媒体用户id
        Integer authorId = dto.getAuthorId();
        //组合key
        String key = BehaviorConstants.BEHAVIOR_FOLLOW + authorId;
//        String dataKey=BehaviorConstants.BEHAVIOR_FOLLOW_DATA_STATISTICS+authorId;
        //获取当前用户id

        Integer userId = Integer.valueOf(AppUserIdHolder.get());

        log.info("当前用户id{}", userId);
        //获取请求状态
        if (dto.getOperation() == BehaviorConstants.BEHAVIOR_USER_FOLLOW) {
            //关注，使用常量BEHAVIOR_FOLLOW加自媒体id 用户id存入set
            cacheService.sAdd(key, String.valueOf(userId));
        } else if(dto.getOperation() == BehaviorConstants.BEHAVIOR_USER_NOTFOLLOW){
            //取消关注
            cacheService.sRemove(key, String.valueOf(userId));
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
